package db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pojo.Properties;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: xiaobing
 * @system name: 工作流引擎
 * @copyright：长安新生（深圳）金融投资有限公司
 * @Date: Created in  2018/9/26 15:39
 */
public class PropertiesDao {

    private static final Logger logger = LoggerFactory.getLogger(PropertiesDao.class);

    public static List<Properties> queryProperties() {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        List<Properties> list = new ArrayList<>();

        try {
            if (conn == null) {
                conn = DBHelper.getConnection();
            }

            //2.获取Statement
            statement = conn.createStatement();
            //3.准备Sql
            String sql = "SELECT * FROM properties WHERE is_flag = 'Y'";
            //4.执行查询，得到ResultSet
            rs = statement.executeQuery(sql);
            while(rs.next()){
                Properties properties = new Properties();
                properties.setProjectCde(rs.getString(1));
                properties.setRemotePath(rs.getString(2));
                properties.setProjectBranch(rs.getString(3));
                properties.setDownloadPath(rs.getString(4));
                list.add(properties);
            }

        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 这里最好要先判断是否为空，不为空了，才去关闭。
                // 而且要从小到大的关闭，顺序不能乱
                if (statement != null) {
                    statement.close();
                }
                if(rs != null) {
                    rs.close();
                }
                /*if (conn != null)
                    conn.close(); */    //注意：如果后续还要使用connection，则不用关闭
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }

        return list;
    }
}
